<template>
  <el-container class="app-container">
    <!-- header -->
    <el-header class="app-header">
      <el-row type="flex" align="middle" justify="space-between">
        <!-- 左侧标题和logo -->
        <el-col :span="8">
          <div class="logo-container">
            <img src="../assets/logo.png" alt="Logo" class="logo-image">
            <h3 class="app-title">充电桩运营管理平台</h3>
          </div>
        </el-col>

        <!-- 右侧用户信息 -->
        <el-col :span="8" style="text-align: right">
          <div class="header-right">
            <el-badge :value="messageCount" :hidden="messageCount === 0" class="notification-badge">
              <i class="el-icon-bell"></i>
            </el-badge>
            <el-divider direction="vertical"></el-divider>
            <el-dropdown trigger="click" @command="handleCommand">
              <span class="user-dropdown">
                <i class="el-icon-user-solid avatar"></i>
                {{ username }}
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="a">
                  <i class="el-icon-user"></i> 个人中心
                </el-dropdown-item>
                <el-dropdown-item command="b">
                  <i class="el-icon-setting"></i> 系统设置
                </el-dropdown-item>
                <el-dropdown-item divided command="c">
                  <i class="el-icon-switch-button"></i> 退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-col>
      </el-row>
    </el-header>

    <el-container class="main-container">
      <!-- aside -->
      <el-aside width="220px" class="app-aside">
        <!-- 菜单组件 -->
        <el-menu
          class="app-menu"
          :default-active="String(activeMenu)"
          background-color="#2c3e50"
          text-color="#e9ecef"
          active-text-color="#ffffff"
          router
        >                                           
          <template v-for="menu in menuList">
            <!-- 
                没有子菜单的直接显示为菜单项 
                只有首页没有子菜单，所以，:index的值需要改成路由路径
             -->
            <el-menu-item 
              v-if="!menu.childrenMenu || menu.childrenMenu.length === 0"
              :key="`menu-${menu.id}`"
              :index="String(menu.link)"
              @click="handleMenuClick(menu)"
                 >
              <i :class="menu.icon"></i>
              <span>{{ menu.name }}</span>
            </el-menu-item>

            <!-- 有子菜单的显示为子菜单 -->
            <el-submenu 
              v-else
              :key="`submenu-${menu.id}`"
              :index="String(menu.id)"
            >
              <template slot="title">
                <i :class="menu.icon"></i>
                <span>{{ menu.name }}</span>
              </template>
              <el-menu-item
                v-for="childMenu in menu.childrenMenu"
                :key="`child-${childMenu.id}`"
                :index="String(childMenu.link)"
                @click="handleMenuClick(childMenu)"
                
                >
                <i :class="childMenu.icon"></i>
                <span>{{ childMenu.name }}</span>
              </el-menu-item>
            </el-submenu>
          </template>
        </el-menu>
      </el-aside>

      <!-- main -->
      <el-main class="app-main">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      username: "admin",
      activeMenu: "1",
      currentPath: "工作台",
      messageCount: 0
    };
  },

  methods: {
    queryUserMenu() {
      this.$axios.get("api/menu/queryMenu").then((resp) => {
        if (resp.data.code == 200) {
          // 过滤掉可能导致重复菜单的数据
          this.menuList = resp.data.data;
        }
      });
    },
    
    handleMenuClick(menu) {
    },
    // 右上角下拉框那一块触发的各种事件
    handleCommand(command) {
      if (command == 'a') {
        // 点击个人中心，跳转到个人中心的页面
        this.$router.push({ name: "PersonalCenter"});
      }
    }
  },
  created() {
    this.queryUserMenu();
  },
};
</script>

<style scoped>
.app-container {
  min-height: 100vh;
  background-color: #edf2f7;
}

.app-header {
  background: linear-gradient(120deg, #00B4DB, #0083B0);
  padding: 0 20px;
  height: 60px !important;
  line-height: 60px;
  position: relative;
  z-index: 10;
  color: white;
}

.logo-image, .app-title, .notification-badge, .user-dropdown, .avatar {
  color: #ffffff;
}

.el-divider--vertical {
  background-color: rgba(255, 255, 255, 0.3);
}

.logo-container {
  display: flex;
  align-items: center;
  height: 60px;
}

.logo-image {
  height: 32px;
  width: auto;
  margin-right: 10px;
}

.app-title {
  color: #ffffff;
  font-size: 18px;
  margin: 0;
  font-weight: 600;
}

.header-right {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  height: 100%;
}

.notification-badge {
  margin-right: 20px;
  cursor: pointer;
  color: #ffffff;
  height: 60px;
  display: flex;
  align-items: center;
}

.notification-badge :deep(.el-badge__content) {
  top: 15px;
  right: -10px;
}

.user-dropdown {
  cursor: pointer;
  display: flex;
  align-items: center;
}

.avatar {
  font-size: 20px;
  margin-right: 8px;
}

.app-aside {
  background-color: #2c3e50;
  position: relative;
  z-index: 9;
  height: calc(100vh - 60px);
  box-shadow: 2px 0 4px rgba(0, 0, 0, 0.1);
}

.app-menu {
  border-right: none;
  padding: 8px;
}

/* 一级菜单样式 */
.app-menu :deep(.el-submenu__title) {
  margin: 4px 0;
  border-radius: 4px;
  font-size: 15px;
  height: 45px !important;
  line-height: 45px !important;
  background: linear-gradient(to right, #2c3e50, #34495e) !important;
  border-left: 4px solid transparent;
}

.app-menu :deep(.el-submenu__title:hover) {
  background: linear-gradient(to right, #34495e, #3d566e) !important;
  border-left: 4px solid #3498db;
}

.app-menu :deep(.el-submenu.is-opened > .el-submenu__title) {
  background: linear-gradient(to right, #34495e, #3d566e) !important;
  border-left: 4px solid #3498db;
  color: #ffffff !important;
}

/* 二级菜单容器 */
.app-menu :deep(.el-menu--inline) {
  background-color: #243342 !important;
  padding: 5px;
  margin: 0;
}

/* 二级菜单项 */
.app-menu :deep(.el-menu-item) {
  height: 40px !important;
  line-height: 40px !important;
  margin: 4px 0;
  padding-left: 48px !important;
  font-size: 14px;
  border-radius: 4px;
  background-color: transparent !important;
  border-left: 3px solid transparent;
}

.app-menu :deep(.el-menu-item:hover) {
  background-color: #2c3e50 !important;
  border-left: 3px solid #3498db;
}

.app-menu :deep(.el-menu-item.is-active) {
  background-color: #3498db !important;
  color: #ffffff !important;
  border-radius: 4px;
}

/* 菜单图标样式 */
.app-menu :deep(.el-menu-item i),
.app-menu :deep(.el-submenu__title i) {
  margin-right: 10px;
  width: 24px;
  text-align: center;
}

.app-main {
  background-color: #edf2f7;
  padding: 24px;
}

.el-dropdown-menu__item i {
  margin-right: 8px;
}

/* 确保子菜单展开时的过渡动画 */
:deep(.el-menu-item-group__title) {
  padding: 0;
}

:deep(.el-submenu.is-opened > .el-submenu__title i) {
  color: #ffffff;
}

/* 优化菜单图标和文字的间距 */
:deep(.el-menu-item span),
:deep(.el-submenu__title span) {
  margin-left: 5px;
}
</style>